import {
  $wuxToptips
} from '../../../dist/index'

var _post_json = require("../../../utils/network.js")._post_json;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    openId : null,
    submitLoading : false,
    weeklyId:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.getStorage({
      key: 'openId',
      success: function (res) {
        that.setData({
          openId: res.data
        })
      }
    });
    that.setData({
      weeklyId: options.weeklyId
    })
    
    console.log("周报Id : " + that.data.weeklyId)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
  formSubmit: function (e) {
    var that = this;

    that.setData({
      submitLoading: true
    })

    console.log('form发生了submit事件，上周内容：', e.detail.value.content)
    console.log('form发生了submit事件，上周内容：', e.detail.value.last_content)
    var content = e.detail.value.content;
    var lastContent = e.detail.value.last_content;
    _post_json("weekly/apply", {
      "openId": this.data.openId,
      "weeklyId": this.data.weeklyId,
      "content": content,
      "lastWeekContent": lastContent

    }).then((res) => {
      wx.showToast({
        "title":"提交成功",
        "success":function(){
          setTimeout(function(){
            wx.navigateBack();
          }, 2000);
        }
      });

    }, (res) => {
      that.setData({
        submitLoading: false
      })
      let respCode = res.data.respCode;
      let respMsg = res.data.respMsg;

      if (respCode == undefined || respCode == 500) {
        $wuxToptips().error({
          hidden: true,
          text: "服务器严重错误",
          duration: 3000,
          success() { },
        })
      }else{
        $wuxToptips().error({
          hidden: true,
          text: respMsg,
          duration: 3000,
          success() { },
        })
      }
    });
  }
})